package com.example.administrator.myapplication;

import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

/**
 * Created by Administrator on 2018/3/8.
 */

public class ImagesListAdapter extends BaseQuickAdapter<ImageBean, BaseViewHolder> {
    private static final int TYPE_ADD = 1;
    private static final int TYPE_NOT_ADD = 2;
    private int selectMax = 9;
    private onAddPicClickListener mOnAddPicClickListener;
    private onItemClickListener mOnItemClicklistener;
    private ViewGroup.LayoutParams mMPara;
    private RequestOptions mOptions1;

    public ImagesListAdapter(int layoutResId, @Nullable List<ImageBean> data) {
        super(layoutResId, data);
        mOptions1 = new RequestOptions();
        mOptions1.placeholder(R.drawable.ic_placeholder);
    }

    private boolean isShowAddItem(int position) {
        int size = getData().size() == 0 ? 0 : getData().size();
        return position == size;
    }


    @Override
    public int getItemCount() {
        if (getData().size() < selectMax) {
            return getData().size() + 1;
        } else {
            return getData().size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_ADD;
        } else {
            return TYPE_NOT_ADD;
        }
    }

    public void setSelectMax(int max) {
        this.selectMax = max;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

    }

    @Override
    protected void convert(final BaseViewHolder helper, ImageBean item) {
        final int position = helper.getAdapterPosition();
        ImageView iv = helper.getView(R.id.fiv);
        mMPara = iv.getLayoutParams();
        if (getItemViewType(position) == TYPE_ADD) {
            Glide.with(mContext)
                    .load(R.drawable.addimg_1x)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            setIvWidth(iv, mMPara);
                            return false;
                        }
                    })
                    .into(iv);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnAddPicClickListener.onAddPicClick();
                }
            });
            helper.getView(R.id.ll_del).setVisibility(View.INVISIBLE);
        } else {
            helper.getView(R.id.ll_del).setVisibility(View.VISIBLE);
            helper.addOnClickListener(R.id.ll_del);

            final LocalMedia media = item.getLocalMedia();
            final ImageBean.ImageNet imageNet = item.getImageNet();
            String path = "";
            if (imageNet != null) {
                if (!TextUtils.isEmpty(imageNet.getImageUrl())) {
                    path = imageNet.getImageUrl();
                }
            } else if (media != null) {
                if (media.isCut() && !media.isCompressed()) {
                    // 裁剪过
                    path = media.getCutPath();
                    Log.d("PATH1", path);
                } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                    // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                    path = media.getCompressPath();
                    Log.d("PATH1", path);
                } else {
                    // 原图
                    path = media.getPath();
                    Log.d("PATH1", path);
                }
            }
            RequestListener<Drawable> drawableRequestListener = new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    setIvWidth(iv, mMPara);
                    return false;
                }
            };
            Glide.with(mContext)
                    .applyDefaultRequestOptions(mOptions1)
                    .load(path)
                    .listener(drawableRequestListener)
                    .into(iv);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int index = helper.getAdapterPosition();
                    mOnItemClicklistener.onItemClick(index);
                }
            });
        }

    }

    private void setIvWidth(ImageView iv2, ViewGroup.LayoutParams para) {
        final int mMeasuredWidth = iv2.getWidth();
        Log.i("haha", "layout width3: " + mMeasuredWidth);
        para.height = mMeasuredWidth;
        iv2.setLayoutParams(para);
    }

    /**
     * 点击添加图片跳转
     */
    public interface onAddPicClickListener {
        void onAddPicClick();
    }

    //点击图片跳转
    public interface onItemClickListener {
        void onItemClick(int position);
    }

    public void setOnAddPicClickListener(onAddPicClickListener onAddPicClickListener) {
        this.mOnAddPicClickListener = onAddPicClickListener;
    }

    public void setOnItemClicklistener(onItemClickListener onItemClicklistener) {
        this.mOnItemClicklistener = onItemClicklistener;
    }

}
